//
//  GSYTabbarViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "GSYTabbarViewController.h"
#import "GSYCustomButton.h"
#import "GSYOtherViewController.h"
@interface GSYTabbarViewController ()
@property (retain,nonatomic)UIVisualEffectView * backgroundview;
@property (retain,nonatomic)NSMutableArray * menuviewsArray;
@property (retain,nonatomic)UIImageView * closeImageview;
@property (retain,nonatomic)UIView * bottomView;
@property(nonatomic,retain)GSYCustomButton *previousBtn;


@end

@implementation GSYTabbarViewController


//自定义设置tabbar是否隐藏
-(void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController
{
//  先删除自己定义的
    [self.tabBarImageView removeFromSuperview];
    
//    如果隐藏了
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        
        [self.tabBar addSubview:self.tabBarImageView];
        
        if (viewController != nil) {
            
            viewController.hidesBottomBarWhenPushed = YES;
        }
        
    }else
    {
        [self.tabBar setHidden:YES];
        
        [self.tabBarImageView setFrame:self.tabBar.frame];
        
        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarImageView = [[UIImageView alloc] init];
    self.tabBarImageView.backgroundColor = [UIColor colorWithWhite:0.898 alpha:1.000];
    self.tabBarImageView.userInteractionEnabled = YES;
    self.tabBarImageView.image = [UIImage imageNamed:@""];
    [self isHiddenTabBar:NO withViewController:nil];
    
    // 下面的方法是调用自定义的生成按钮的方法
    //发现按钮
    
    [self createButtonWithNormalName:@"TabBarlconDiscoveryNormal" andSelectName:@"TabBarlconDiscovery" andTitle:@"发现" andIndex:0];
    
    
    //中心按钮
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    centerButton.frame = CGRectMake(0, 0, 41, 41);
    centerButton.backgroundColor = [UIColor whiteColor];
    centerButton.center = CGPointMake(375 / 2, self.tabBarImageView.frame.size.height / 2);
    [centerButton setBackgroundImage:[UIImage imageNamed:@"share_compose_add_photo"] forState:UIControlStateNormal];
    centerButton.adjustsImageWhenHighlighted = NO;
    centerButton.layer.cornerRadius = 20.5;
    centerButton.layer.masksToBounds = YES;
    [centerButton addTarget:self action:@selector(centerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarImageView addSubview:centerButton];
    
    //目的地按钮
    

//    [self createButtonWithNormalName:@"TabBarIconDestinationNormal" andSelectName:@"TabBarIconDestination" andTitle:@"目的地" andIndex:1];
    
//    [self createButtonWithNormalName:@"iconfont-diantai (1)" andSelectName:@"iconfont-diantai" andTitle:@"电台" andIndex:2];
    
    
    
    
    
    GSYCustomButton *button = [GSYCustomButton buttonWithType:UIButtonTypeCustom];
    
    button.adjustsImageWhenHighlighted = NO;
    
    button.tag = 1;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width/4;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    
    button.frame = CGRectMake(0, 0, buttonW, buttonH);
    
    button.center = CGPointMake(375/3, self.tabBarImageView.frame.size.height/2);
    
    [button setImage:[UIImage imageNamed:@"TabBarIconDestinationNormal"] forState:UIControlStateNormal];
    
    [button setImage:[[UIImage imageNamed:@"TabBarIconDestination"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button setTitle:@"目的地" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1] forState:UIControlStateDisabled];
    
    button.tintColor = [UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1];
    
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    button.titleLabel.font = [ UIFont systemFontOfSize:11]; // 设置标题的字体大小
    [self.tabBarImageView addSubview:button];
    
    
    
    GSYCustomButton *button1 = [GSYCustomButton buttonWithType:UIButtonTypeCustom];
    
    button1.adjustsImageWhenHighlighted = NO;
    
    button1.tag = 2;
    
    button1.frame = CGRectMake(0, 0, buttonW, buttonH);
    
    button1.center = CGPointMake(375/1.5, self.tabBarImageView.frame.size.height/2);
    
    [button1 setImage:[UIImage imageNamed:@"iconfont-diantai (1)"] forState:UIControlStateNormal];
    
    [button1 setImage:[[UIImage imageNamed:@"iconfont-diantai"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button1 setTitle:@"电台" forState:UIControlStateNormal];
    
    [button1 setTitleColor:[UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1] forState:UIControlStateDisabled];
    
    button1.tintColor = [UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1];
    
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button1 addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    button1.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    button1.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    button1.titleLabel.font = [ UIFont systemFontOfSize:11]; // 设置标题的字体大小
    [self.tabBarImageView addSubview:button1];

    
    
    
    
    [self createButtonWithNormalName:@"iconfont-xiaoren.png" andSelectName:@"iconfont-xiaoren (1).png" andTitle:@"我的" andIndex:3];
    
    //默认选中第一个按钮
    GSYCustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮


    
    
}
-(void)createButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index
{


    GSYCustomButton *button = [GSYCustomButton buttonWithType:UIButtonTypeCustom];
    
    button.adjustsImageWhenHighlighted = NO;
    
    button.tag = index;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width/4;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    
    button.frame = CGRectMake(0, 0, buttonW, buttonH);
    
    button.center = CGPointMake(375/8+ 375/4*index, self.tabBarImageView.frame.size.height/2);
    
    [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    
    [button setImage:[[UIImage imageNamed:selected]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1] forState:UIControlStateDisabled];
    
    button.tintColor = [UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1];
    
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    button.titleLabel.font = [ UIFont systemFontOfSize:11]; // 设置标题的字体大小
    [self.tabBarImageView addSubview:button];

    

}

#pragma mark 按钮被点击时调用
- (void)changeViewController:(GSYCustomButton *)sender

{
    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if (self.previousBtn != sender) {
        
//        让点
        self.previousBtn.enabled = YES ;
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}


#pragma mark 中间按钮的点击事件
- (void)centerAction:(UIButton *)button {
    
    UIVisualEffectView * blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    self.backgroundview = blurView;
    [blurView setFrame:self.view.frame];
    [self.view addSubview:blurView];
    [self showMenus];
}
-(void)showMenus
{

    NSArray * imagesNameArray = @[@"ic_poi_scenery", @"ic_poi_hotel", @"ic_poi_eat", @"ic_poi_entertainment", @"ic_poi_shopping", @"ic_poi_others"];
    
     NSArray * labelsArray = @[@"景点", @"住宿", @"餐厅", @"休闲娱乐", @"购物", @"全部"];
    
    CGFloat width = self.view.frame.size.width;
    
    CGFloat itemWidth = width/3;
    
    self.menuviewsArray = [NSMutableArray new];
//    循环创建6个View
    for (int index = 0; index<6; index++) {
//        下面view动画用到每个的动画执行的间隔
        NSTimeInterval delay = 0.03 *index;
        
        CGFloat centerx;
        
        CGFloat centery;
        
        if (index < 3) {
            centerx = width /6 * (2*index + 1);
            centery = self.view.frame.size.height - itemWidth - itemWidth - itemWidth/2 - 49;
        }else{
            centerx = width/6 * (2 *(index - 2) -1);
            centery = self.view.frame.size.height - itemWidth - itemWidth/2 - 49;
        }
         UIImage * image  = [UIImage imageNamed:imagesNameArray[index]];
        
         NSString * title = labelsArray[index];
        
        UIView *containview = [self createOneMenuViewWithImage:image Title:title];

//        起始点让他在屏幕下面达到点击按钮飞上来的效果
        containview.center = CGPointMake(centerx, self.view.frame.size.height);
        
          UITapGestureRecognizer * menuTab = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(catchMenuTap:)];
        
        [containview addGestureRecognizer:menuTab];
        
        [self.view addSubview:containview];
        
        [self.menuviewsArray addObject:containview];
        
        containview.tag = index;
        
        //UIViewAnimationOptionCurveEaseInOut   时间曲线函数，由慢到快
        [UIView animateWithDuration:0.3 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
            containview.center = CGPointMake(centerx, centery - 30);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                containview.center = CGPointMake(centerx, centery);
            } completion:^(BOOL finished) {
                
            }];
        }];

    }

//   点击按钮后，tabbar的颜色
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49)];
    
    bottomView.backgroundColor = [UIColor colorWithWhite:0.875 alpha:1.000];
    
//    中间的按钮
    UIImageView * closeImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 41, 41)];
    
    [bottomView addSubview:closeImageview];
    
    closeImageview.backgroundColor = [UIColor whiteColor];
    
    closeImageview.layer.cornerRadius = 20.5;
    
    closeImageview.layer.masksToBounds = YES;
    
    //图片
    closeImageview.image = [UIImage imageNamed:@"share_compose_add_photo"];
    
    closeImageview.center = CGPointMake(CGRectGetWidth(bottomView.bounds)/2, CGRectGetHeight(bottomView.bounds)/2-0.5);
    
     self.closeImageview = closeImageview;
    
    //添加手势
    UITapGestureRecognizer * closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenus)];
    
    closeImageview.userInteractionEnabled = true;
    
    [closeImageview addGestureRecognizer:closeTap];
    
    [self.view addSubview:bottomView];
    
    self.bottomView = bottomView;
    
//    旋转
    [UIView animateWithDuration:0.4 animations:^{
        closeImageview.transform = CGAffineTransformMakeRotation(M_PI / 4);
    } completion:^(BOOL finished) {
        
    }];

}
#pragma mark 隐藏菜单栏
-(void)hideMenus{
    
    [UIView animateWithDuration:0.4 animations:^{
        self.closeImageview.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self.bottomView removeFromSuperview];
        [self.backgroundview removeFromSuperview];
    }];
    for (NSInteger index = 5; index >= 0; index--) {
        UIView * containview = self.menuviewsArray[index];
        CGFloat  dealy = (5 - index) * 0.03;
        [UIView animateWithDuration:0.2 delay:dealy options:UIViewAnimationOptionCurveLinear animations:^{
            containview.center = CGPointMake(containview.center.x, self.view.frame.size.height + CGRectGetWidth(containview.frame)/2);
        } completion:^(BOOL finished) {
            [containview removeFromSuperview];
        }];
    }
    self.menuviewsArray = nil;
}


-(void)catchMenuTap:(UITapGestureRecognizer *)tap{
    
    GSYOtherViewController *otherVC = [[GSYOtherViewController alloc]init];
    
    if (tap.view.tag == 5) {
        
        otherVC.initNum = 0;
        
    } else {
        
        otherVC.initNum = tap.view.tag + 1;
    }
    
    UINavigationController *otherNavC = [[UINavigationController alloc] initWithRootViewController:otherVC];
    
    [self presentViewController:otherNavC animated:YES completion:nil];
    
    [self hideMenus];

    
    
}


-(UIView *)createOneMenuViewWithImage:(UIImage *)image Title:(NSString *)title
{

    CGFloat width = self.view.frame.size.width;
    
    CGFloat itemWidth = width/5;
    
    UIView *containview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, itemWidth, itemWidth)];
    
    UIImageView *iconImageiew = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, itemWidth-10, itemWidth-10)];
    
    iconImageiew.center = CGPointMake(itemWidth/2, itemWidth/2);
    
//    充满整个父视图
    iconImageiew.contentMode = UIViewContentModeScaleAspectFill;
    
    iconImageiew.image = image;
    
    [containview addSubview:iconImageiew];

    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, itemWidth, itemWidth, 20)];
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    [containview addSubview:label];
    
    return containview;



}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
